package engine.systems.motion.draggable
{
	import ash.core.Component;
	import ash.core.Entity;
	
	import flash.events.MouseEvent;
	
	import org.osflash.signals.Signal;
	
	public class Draggable extends Component
	{
		internal var _locked:Boolean;
		
		public var drag:Signal = new Signal(Entity);
		public var drop:Signal = new Signal(Entity);
		
		internal var _dragging:Boolean 		= false;
		internal var _invalidate:Boolean 	= false;
		
		internal var _offsetX:Number = 0;
		internal var _offsetY:Number = 0;
		
		public var axis:int;
		public var forward:Boolean;
		
		public function Draggable(axis:int = 2, forward:Boolean = true)
		{
			this.axis 		= axis;
			this.forward 	= forward;
		}
		
		override public function destroy():void
		{
			this.drag.removeAll();
			this.drag = null;
			
			this.drop.removeAll();
			this.drop = null;
		}
		
		public function get locked():Boolean { return this._locked; }
		public function set locked(locked:Boolean):void
		{
			this._locked = locked;
			
			if(locked && this._dragging)
			{
				this._dragging 		= false;
				this._invalidate 	= true;
			}
		}
		
		public function get dragging():Boolean { return this._dragging; }
		public function set dragging(dragging:Boolean):void
		{
			if(this._locked) return;
			
			if(this._dragging != dragging)
			{
				this._dragging 		= dragging;
				this._invalidate 	= true;
			}
		}
		
		internal function onDrag(entity:Entity, event:MouseEvent):void
		{
			if(this._locked) return;
			
			this._dragging 		= true;
			this._invalidate 	= true;
		}
		
		internal function onDrop(entity:Entity, event:MouseEvent):void
		{
			if(this._locked) return;
			
			this._dragging 		= false;
			this._invalidate 	= true;
		}
	}
}